package com.sjsoft.application;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.sjsoft.adapter.VideoAdapter;
import com.sjsoft.model.CSubCategory;
import com.sjsoft.model.SmsItem;
import com.sjsoft.model.messageItem;
import com.sjsoft.model.videoItem;
import com.sjsoft.webservice.GetVersionThread;

import android.app.Application;




public class chatVideoApp extends Application{

	public ArrayList<CSubCategory> videoByCategory;
	public ArrayList<videoItem> arrVideoAtExtendId;
	public static ArrayList<messageItem> arrMessage;
	public ArrayList<Integer>arrPosInitTab;
	public ArrayList<SmsItem>arrSMS;
	
	public ArrayList<ArrayList<videoItem>>arrAdapterInit;
	public int nPosFlip = 0; //count for each flip board
	public boolean bLastData = false;	
	public ArrayList<videoItem> arrTopVideo;
	public String sPrefixCode;
	public String sPrefixName;
	public String sImei;
	public String sVersion;
	public int nTotalAmount = 0;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	
		arrVideoAtExtendId = new ArrayList<videoItem>();
		videoByCategory = new ArrayList<CSubCategory>();
		arrPosInitTab = new ArrayList<Integer>();
		arrMessage = new ArrayList<messageItem>();
		arrAdapterInit = new ArrayList<ArrayList<videoItem>>();
		
		GetVersionThread.state = GetVersionThread.IDLE;
		
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext())
				.threadPoolSize(3)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.memoryCacheSize(1500000000)
				// 1.5 Mb
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.enableLogging() // Not necessary in common
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
		

	}
	
	public void pushVideo(ArrayList<videoItem> arr)
	{
		
		arrVideoAtExtendId.addAll(arr);

	}
		
	
	public void clearListVideo()
	{
		arrVideoAtExtendId.clear();
	}
	
	public void addPosTab(Integer nPos)
	{
		if(arrPosInitTab.contains(nPos) == false)
			arrPosInitTab.add(nPos);
	}
	
	public int getRealTab(Integer nPos)
	{
		return arrPosInitTab.indexOf(nPos);
	}
	public SmsItem getSms5()
	{
		if(arrSMS != null)
		{
			int nSize = arrSMS.size();
			for(int i =0 ; i < nSize; i++)
			{
				SmsItem item = arrSMS.get(i);
				if(item != null && item._type == 1)
				{
					return item;
				}
			}
			
		}
			return null;
	}
	
	public SmsItem getSms10()
	{
		if(arrSMS != null)
		{
			int nSize = arrSMS.size();
			for(int i =0 ; i < nSize; i++)
			{
				SmsItem item = arrSMS.get(i);
				if(item != null && item._type == 2)
				{
					return item;
				}
			}
			
		}
			return null;
	}
	public SmsItem getSms15()
	{
		if(arrSMS != null)
		{
			int nSize = arrSMS.size();
			for(int i =0 ; i < nSize; i++)
			{
				SmsItem item = arrSMS.get(i);
				if(item != null && item._type == 3)
				{
					return item;
				}
			}
			
		}
			return null;
	}
	public static void appendLog(String text)
	{       
		File logFile = new File("sdcard/log.file");
		   if (!logFile.exists())
		   {
		      try
		      {
		         logFile.createNewFile();
		      } 
		      catch (IOException e)
		      {
		         // TODO Auto-generated catch block
		         e.printStackTrace();
		      }
		   }
		   try
		   {
		      //BufferedWriter for performance, true to set append to file flag
		      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
		      buf.append(text);
		      buf.newLine();
		      buf.close();
		   }
		   catch (IOException e)
		   {
		      // TODO Auto-generated catch block
		      e.printStackTrace();
		   }
	}
}
